package servlet;

import dao.CountMapper;
import org.apache.ibatis.session.SqlSession;
import utils.MybatisUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/addServlet")
public class addServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");  // 设置request字符编码
        response.setContentType("text/json; charset=utf-8");    // 设置response的编码及格式
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Cache-Control","no-cache");
        PrintWriter out = response.getWriter();
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        try {
            CountMapper CountMapper = sqlSession.getMapper(CountMapper.class);
            HashMap<String, Object> map = new HashMap<String, Object>();
            Map parameterMap = request.getParameterMap();
            String project = request.getParameter("project");
            String type = request.getParameter("type");
            String money = request.getParameter("money");
            String date = request.getParameter("date");

            System.out.println(type);
            map.put("project",project);
            map.put("type",type);
            map.put("money",money);
            map.put("date",date);
            CountMapper.addCountMap(map);
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            sqlSession.close();
        }
        response.sendRedirect("/count.html");
    }
}
